package com.poposchool.project.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.Quarter;
import com.poposchool.project.common.Result;
import com.poposchool.project.entity.TotalUser;
import com.poposchool.project.service.ITotalUserService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author Wang Jie
 * @Date 2022/7/2 20:35
 * @Version 1.0
 */
@RestController
@RequestMapping("/echarts")
public class EchartsController {
    @Resource
    private ITotalUserService userService;

    @GetMapping("/bar")
    public Result get(){
        Map<String,Object> map = new HashMap<>();
        map.put("x", CollUtil.newArrayList("周一","周二","周三","周四","周五","周六","周日"));
        map.put("y", CollUtil.newArrayList(12,4,32,23,14,31,15));
        return Result.success(map);
    }
    
    @GetMapping("/members")
    public Result members(){
        List<TotalUser> users = userService.list();

        int q1=0,q2=0,q3=0,q4=0;
        for (TotalUser user : users){
            //把localDataTime转化为data
            ZoneId zoneId = ZoneId.systemDefault();
            ZonedDateTime zdt = user.getCreateTime().atZone(zoneId);
            Date createTime;
            Quarter quarter = null;
            if (zdt != null){
                createTime = Date.from(zdt.toInstant());
                quarter = DateUtil.quarterEnum(createTime);
            }
            switch (quarter){
                case Q1:
                    q1++;
                    break;
                case Q2:
                    q2++;
                    break;
                case Q3:
                    q3++;
                    break;
                case Q4:
                    q4++;
                    break;
            }
        }
        return Result.success(CollUtil.newArrayList(q1,q2,q3,q4));
    }
}
